/*
 * @Description: 
 * @Author: paddies
 * @Date: 2023-06-01 13:38:13
 * @LastEditors: paddies
 * @LastEditTime: 2023-06-15 00:43:48
 * @FilePath: /flutter-cli/lib/utils/global_prefs.dart
 */
import 'package:flutter/cupertino.dart';
import 'package:shared_preferences/shared_preferences.dart';

class GlobalPrefs {
  static const String userName = ""; //用户名
  static const String userToken = ""; //token
  static late SharedPreferences _prefs; //延迟初始化
  static Future<String> init() async {
    WidgetsFlutterBinding.ensureInitialized();
    _prefs = await SharedPreferences.getInstance();
    return 'ok';
  }

  static void saveValue(String key, String value) {
    _prefs.setString(key, value);
  }

  static getValue(String key) {
    return _prefs.getString(key);
  }

  static void removeValue(String key) {
    _prefs.remove(key);
  }

  static void saveUserName(String userName) {
    _prefs.setString(userName, userName);
  }

  static String? getUserName() {
    return _prefs.getString(userName);
  }

  static void saveToken(String token) {
    _prefs.setString(userToken, token);
  }

  static String? getToken() {
    return _prefs.getString(userToken);
  }

  static void removeUserName() {
    _prefs.remove(userName);
  }

  static void removeToken() {
    _prefs.remove(userToken);
  }

  static void clearGlobal() {
    _prefs.remove("userPhone");
    _prefs.clear();
  }
}
